package com.example.springbootjpaintegrat.controller;

import com.example.springbootjpaintegrat.conversion.UserConversion;
import com.example.springbootjpaintegrat.model.User;
import com.example.springbootjpaintegrat.service.UserService;
import com.openapi.open.controller.UsersApiDelegate;
import com.openapi.open.controller.model.OpenCreateUserRequest;
import com.openapi.open.controller.model.OpenUpateUserRequest;
import com.openapi.open.controller.model.OpenUser;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

/**
 * 实现生成的接口，然后实现对应的服务既可
 */
public class UsersApiDelegateImpl implements UsersApiDelegate {

    private final UserService userService;
    private final UserConversion userConversion;

    public UsersApiDelegateImpl(UserService userService,UserConversion userConversion) {
        this.userService = userService;
        this.userConversion = userConversion;
    }

    @Override
    public ResponseEntity<OpenUser> createUser(OpenCreateUserRequest openCreateUserRequest) {
        User user = this.userService.createUser(this.userConversion.toUser(openCreateUserRequest));
        return ResponseEntity.status(HttpStatus.CREATED).body(this.userConversion.toControllerOpenUser(user));
    }

    @Override
    public ResponseEntity<Void> deleteUser(String id) {
        this.userService.deleteUser(id);
        return ResponseEntity.ok().build();
    }

    @Override
    public ResponseEntity<OpenUser> upateUser(String id, OpenUpateUserRequest openUpateUserRequest) {
        User user = this.userService.upateUser(id, this.userConversion.toUser(openUpateUserRequest));
        return ResponseEntity.status(HttpStatus.CREATED).body(this.userConversion.toControllerOpenUser(user));
    }
}
